<?php

namespace App\Http\Request;

use App\Traits\ApiRespond;

use Illuminate\Validation\ValidationException;

class Store
{
    use  ApiRespond;


    private $request;

    /**
     *
     * 验证规则
     * Created by Paco
     * Date: 2018/8/13
     * Time: 23:41
     * @throws ValidationException
     */
    protected function createDefaultValidator()
    {
        $validator = app('validator')->make(
            $this->validateData(),
            $this->rules(),
            $this->messages(), $this->attributes()
        );

        $errors = $validator->errors();
        if (count($errors->keys())) {
            $this->failedValidation($validator, $errors);
        }

        $this->after();

    }


    /**
     *
     * 规则有错误则则返回第一条错误
     * Created by Paco
     * Date: 2018/8/13
     * Time: 22:49
     * @throws ValidationException
     */
    protected function failedValidation($validator, $errors)
    {

        $respond = null;
        $key = $errors->keys()[0];
        $msg = $errors->first($key);

        if (is_numeric($msg)) {
            $respond = $this->respondError($msg, $key);
        } elseif (is_string($msg)) {
            $arr = explode('|', $msg);
            if (count($arr) > 1 && is_numeric($arr[0])) {
                $respond = $this->respondWithMessage($arr[1], null, $arr[0], $key);
            } else {
                $respond = $this->respondWithMessage($msg, null, null, $key);
            }
        } else {
            $respond = $this->respondError();
        }
        throw (new ValidationException($validator, $respond));

    }


    /**
     *
     * 验证方法
     * @author Paco
     * @createTime 2018/8/16 10:25
     * @param $request
     * @return mixed
     * @throws ValidationException
     */
    public function validated($request)
    {
        $this->request = $request;
        $this->createDefaultValidator();
        return $this->validateData();
    }

    /**
     *
     * 获取请求方法类型
     * @author Paco
     * @createTime 2018/8/16 10:28
     * @return mixed
     */
    public function method()
    {
        return $this->request->method();
    }


    /**
     *
     * 获取请求数据
     * @author Paco
     * @createTime 2018/8/16 10:33
     * @return mixed
     */
    public function validateData()
    {
        return $this->request->all();
    }

    /**
     *
     * 获取请求的URI
     * @author Paco
     * @createTime 2018/8/16 10:30
     * @return mixed
     */
    public function uri()
    {
        return $this->request->uri();
    }


    /**
     *
     * 验证字段出错描术信息
     * Created by Paco
     * Date: 2018/8/13
     * Time: 23:39
     * @return array
     */
    protected function messages()
    {
        return [];
    }

    /**
     *
     * 验证属性名称
     * Created by Paco
     * Date: 2018/8/13
     * Time: 23:39
     * @return array
     */
    protected function attributes()
    {
        return [];
    }


    /**
     *
     * 验证规则
     * Created by Paco
     * Date: 2018/8/13
     * Time: 23:38
     * @return array
     */
    protected function rules()
    {
        return [];
    }


    /**
     *
     * 验证后回调方法
     * Created by Paco
     * Date: 2018/8/13
     * Time: 23:40
     * @return bool
     */
    protected function after()
    {
        return true;
    }
}
